<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
    </style>
</head>

<body>
    声明局部变量<input type="checkbox" name="bl" checked="true" onchange="jsonInput()">
    取数据文本<input type="checkbox" name="show" checked="true" onchange="jsonInput()">
    <br />
    <label><textarea name="json" oninput="jsonInput()" rows="40" cols="60"></textarea></label>
    <label><textarea name="jsonShow" rows="40" cols="60"></textarea></label>
    <label><textarea name="ecode" rows="40" cols="60"></textarea></label>
    <br />
    <br />
    <label name="errorCode" style="color:red;"></label>
    <br />
    <br />
    <!-- 下面一行可以删除 -->
    <button onclick="test()">填入测试JSON</button>
</body>
<script>
    // 该方法可以删除
    function test() {
        var e = document.querySelector("[name=json]");
        e.value = JSON.stringify({ "code": 200, "data": { "token": "c437fbdc-3cea-4b9a-9d41-a3937e020e74", "token_type": "Bearer", "expiration": 1655371207 }, "b": true, "c": [123, "asd", { "asd": "123" }] });
        jsonInput();
    }

    function jsonInput() {
        var e = document.querySelector("[name=json]");
        var ecode = document.querySelector("[name=ecode]");
        var jsonShow = document.querySelector("[name=jsonShow]");
        var errorCode = document.querySelector("[name=errorCode]");
        errorCode.innerText = "";
        jsonShow.value = "";
        ecode.value = "";
        try {
            var obj = JSON.parse(e.value.trim());
        } catch (error) {
            var regex = /position (\d+)/;
            var line = error.message.match(regex);
            if (line == null) {
                errorCode.innerText = "JSON结尾部分出错";
            }
            else {
                errorCode.innerText = "JSON从以下位置开始出错:" + e.value.substr(line[1]);
            }
            ecode.value = error;
            return;
        }
        jsonShow.value = JSON.stringify(obj, "", " ");
        var result = ".版本 2\r\n\r\n";
        if (document.querySelector("[name=bl]").checked) {
            result += ".局部变量 json, 类_json\r\n\r\n";
        }
        function each(o, parent, array) {
            for (item in o) {
                var strItem = item;
                strItem = array ? "[" + strItem + "]" : "['" + strItem + "']";
                var p = parent == null ? strItem : parent + strItem;
                if (typeof o[item] == "string" || typeof o[item] == "bigint") {
                    result += "json.置属性 (“" + p + "”, “" + o[item].replaceAll('"', '"+#引号+"') + "”, )\n";
                }
                else if (typeof o[item] == "number" || typeof o[item] == "boolean") {
                    result += "json.置属性 (“" + p + "”, “" + o[item] + "”, 真)\n";
                } else if (typeof o[item] == "object") {
                    if (Array.isArray(o[item])) {
                        result += "json.置属性 (“" + p + "”, “[]”, 真)\n";
                        each(o[item], parent == null ? "['" + item + "']" : parent + "['" + item + "']", true);
                    } else {
                        result += "json.置属性 (“" + p + "”, “{}”, 真)\n";
                        each(o[item], parent == null ? "['" + item + "']" : parent + "['" + item + "']");
                    }
                } else if (typeof o[item] == "undefined") {
                    result += "json.置属性 (“" + p + "”, “undefined”, 真)\n";
                }
            }
        }
        if (typeof obj == "string" || typeof obj == "bigint") {
            result += "json.置属性(“”, “" + obj.replaceAll('"', '"+#引号+"') + "”, )\n";
        } else if (typeof obj == "number" || typeof obj == "boolean") {
            result += "json.置属性(“”, “" + obj + "”)\n";
        } else if (typeof obj == "object") {
            if (Array.isArray(obj)) {
                if (typeof obj[0] == "object") {
                    result += "json.加成员 (“{}”, “”, 真, )\n";
                }
                each(obj, null, true);
            } else {
                each(obj);
            }
        }
        else if (typeof o[item] == "undefined") {
            result += "json.置属性 (“”, “undefined”)\n";
        }
        if (document.querySelector("[name=show]").checked) {
            result += "调试输出(json.取数据文本 ())\n";
        }
        ecode.value = result;
    }
</script>

</html>